class Dog extends EventEmitter{
  constructor(name,energy){
    this.#name = name;
    this.#energy = energy;
    const timer = setInterval(()=>{
      if(this.#energy < 0){
        clearInterval(timer);
      }else{
        this.emit('bark',{
          name:this.#name,
          energy:this.#energy--
        });    
       }
    },1000)
  }
  on(eventName,lisenter){  //addEventLisenters 订阅事件
    if(typeof this.#lisenters[eventName] === 'undefined'){
      this.#lisenters[eventName] = [];
    }
    this.#lisenters[eventName].push(lisenter);
  }

  emit(eventName,argument){  //发出事件
    this.#lisenters[eventName].forEach(fn{
      fn.call(this,argument);
    }) 
  }

  #lisenters = {};
  #name = '';
  #energy = 0;
}
module.exports = Dog;
